﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Data.Linq.SqlClient;
using RelightERP.DAL.EntityModel;
using RelightERP.BLL;
using RelightERP.BO.Entities;

public partial class Admin_UserControls_Member : System.Web.UI.UserControl
{
    MasterHelper masterHelper = null;

    #region EVENT HANDLERS

    protected void Page_Init(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadDataSource();
            if (masterHelper == null) masterHelper = new MasterHelper();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateYearDropDown(ref ddlDOBYear);
            PopulateMonthDropDown(ref ddlDOBMonth);
            PopulateDayDropDown(ref ddlDOBDate);
            LoadRankDropDown();
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ResetForm();
        Utility.HideMsgDiv(divMsg);
        Utility.HideMsgDiv(divErr);
        tbAppFormNo.Focus();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //if (!Page.IsValid)
        //    return;
        if (IsValidDOB())
        { ;}
    }

    protected void DOBDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblAgeValidationMsg.Visible = false;
        if (IsValidDOB())
        {
            //Assuming actual date value will start from Index = 1
            if (ddlDOBDate.SelectedIndex > 0 && ddlDOBMonth.SelectedIndex > 0 && ddlDOBYear.SelectedIndex > 0)
            {
                DateTime dtDOB = new DateTime(Convert.ToInt32(ddlDOBYear.SelectedValue), Convert.ToInt32(ddlDOBMonth.SelectedValue), Convert.ToInt32(ddlDOBDate.SelectedValue));
                if (dtDOB != null)
                {
                    TimeSpan tsAge = DateTime.Today.Subtract(dtDOB);
                    lblAge.Text = "Age: " + SqlMethods.DateDiffYear(dtDOB, DateTime.Today).ToString() + " yrs.";
                }
            }
            else if (ddlDOBDate.SelectedIndex <= 0 && ddlDOBMonth.SelectedIndex <= 0 && ddlDOBYear.SelectedIndex <= 0)
            {
                lblAge.Text = string.Empty;
            }
        }
        else
        {
            lblAgeValidationMsg.Visible = true;
            lblAge.Text = string.Empty;
        }

        (sender as DropDownList).Focus();
    }

    #endregion

    #region PRIVATE METHODS

    #region DOB

    private void PopulateYearDropDown(ref DropDownList ddlYear)
    {
        ListItem liYear = new ListItem();
        liYear.Text = "YYYY";
        liYear.Value = "0000";
        ddlYear.Items.Add(liYear);
        int currentYear = DateTime.Now.Year;
        for (int i = 1900; i <= currentYear; i++)
        {
            liYear = new ListItem();
            liYear.Text = i.ToString();
            liYear.Value = i.ToString();
            ddlYear.Items.Add(liYear);
        }
    }

    private void PopulateMonthDropDown(ref DropDownList ddlMonth)
    {
        ListItem liMonth = new ListItem();
        liMonth.Text = "MM";
        liMonth.Value = "00";
        ddlMonth.Items.Add(liMonth);
        string monthText;
        for (int i = 1; i <= 12; i++)
        {
            if (i < 10)
                monthText = "0" + i.ToString();
            else
                monthText = i.ToString();
            liMonth = new ListItem();
            liMonth.Text = monthText.ToString();
            liMonth.Value = monthText.ToString();
            ddlMonth.Items.Add(liMonth);
        }
    }

    private void PopulateDayDropDown(ref DropDownList ddlDay)
    {
        ListItem liDay = new ListItem();
        liDay.Text = "DD";
        liDay.Value = "00";
        ddlDay.Items.Add(liDay);
        string dayText;
        for (int i = 1; i <= 31; i++)
        {
            if (i < 10)
                dayText = "0" + i.ToString();
            else
                dayText = i.ToString();
            liDay = new ListItem();
            liDay.Text = dayText.ToString();
            liDay.Value = dayText.ToString();
            ddlDay.Items.Add(liDay);
        }
    }

    private bool IsValidDOB()
    {
        bool isValid = false;

        if (ddlDOBDate.SelectedIndex <= 0 || ddlDOBMonth.SelectedIndex <= 0 || ddlDOBYear.SelectedIndex <= 0)
        {
            return false;
        }

        if (ddlDOBDate.SelectedIndex > 0 && ddlDOBMonth.SelectedIndex > 0 && ddlDOBYear.SelectedIndex > 0)
        {
            try
            {
                DateTime dtDOB = new DateTime(Convert.ToInt32(ddlDOBYear.SelectedValue), Convert.ToInt32(ddlDOBMonth.SelectedValue), Convert.ToInt32(ddlDOBDate.SelectedValue));
                isValid = true;
            }
            catch
            {
                isValid = false;
            }
        }

        return isValid;
    }
    #endregion

    private void LoadRankDropDown()
    {
        using (RelightDBEntities context = new RelightDBEntities())
        {
            IEnumerable<Ranks> rankList = context.Ranks.OrderByDescending(x => x.RankPosition).Where(x => x.RankPosition != 0);
            ddlRank.DataSource = rankList;
            ddlRank.DataTextField = "RankName";
            ddlRank.DataValueField = "RankID";

            ddlRank.DataBind();
        }
    }

    private void ResetForm()
    {
        lblAgeValidationMsg.Visible = false;
        //tbAddress.Text = string.Empty;
        //tbBranchName.Text = string.Empty;
        //tbPhone1.Text = string.Empty;
        //tbPhone2.Text = string.Empty;
        tbAppFormNo.Text = string.Empty;
        tbDOJ.Text = string.Empty;
        tbMemberCode.Text = string.Empty;
        ddlRank.SelectedIndex = 0;
        tbIntroducerCode.Text = string.Empty;
        //lblIntroducerName.te

        btnSave.CommandArgument = "0";
        btnSave.CommandName = "InsertMember";
    }

    #endregion
    
    
}